package com.cssw.fyzb.job.executor.service.team;

import com.cssw.fyzb.core.dao.team.UserTreeDao;
import com.cssw.fyzb.core.dao.user.UserDao;
import com.cssw.fyzb.core.mao.task.DailyTaskMao;
import com.cssw.fyzb.core.model.dodb.User;
import com.cssw.fyzb.core.model.dodb.UserTree;
import com.cssw.fyzb.core.model.dodb.UserTreeH;
import com.cssw.fyzb.core.model.mos.DailyTaskMo;
import com.cssw.fyzb.core.util.DateUtil;
import com.cssw.fyzb.core.util.StringUtil;
import com.cssw.fyzb.core.util.TimeUtil;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @description:
 * @author: zzf
 * @time: 2021/1/10 15:49
 */
@Service
public class UserTreeServiceImpl implements UserTreeService {

    private static final Logger logger = LoggerFactory.getLogger(UserTreeServiceImpl.class);

    @Autowired
    private UserTreeDao userTreeDao;

    @Autowired
    private DailyTaskMao dailyTaskMao;

    @Autowired
    private UserDao userDao;

    private static final int pageSize = 1000; //条数

    private static final int viewLeng = 300;

    @Override
    public void teamInspection() {

        int pageNumber = 1;
        int pageSize = 500;

        //3天内没有做任务，需要解散团队， 因为是凌晨2点执行，所以写4天前
        Integer pastDay = TimeUtil.getPastYYMMDD(4);

        long currMills = System.currentTimeMillis();
        long addMills = 3 * 24 * 60 * 60 * 1000;
        logger.info("begin team job....");
        while (true) {
            Page<User> pages = userDao.page(pageNumber, pageSize);
            if (pages.getList() == null || pages.getList().size() <= 0) {
                break;
            }
            pageNumber++;

            for (User user : pages.getList()) {
                try {
                    //未实名，直接跳过
                    if (!user.isFaceAuth()) {
                        continue;
                    }

                    if (null == user.getCreateTime()) {
                        continue;
                    }

                    //如果用户注册小于3天，不做解散
                    long createMills = user.getCreateTime().getTime();
                    if (currMills < createMills + addMills) {
                        continue;
                    }

                    //从mongo中查询用户最后观看数据
                    DailyTaskMo mo = dailyTaskMao.findByUserId(user.getUserId());
                    //如果没有看过视频,解散
                    if (null == mo) {
                        saveUserTreeH(user.getUserId());
                    } else{
                        //如果最后观看的日期，小于最后的日期
                        Integer lastViewDay = Integer.parseInt(mo.getViewDay());
                        if (lastViewDay < pastDay){
                            saveUserTreeH(user.getUserId());
                        }
                    }
                } catch(Exception e){
                    logger.error("", e);
                    logger.info(">>>>teamJob:" + user.getUserId());
                }
            }
        }
        logger.info("end team job....");
    }


    private void saveUserTreeH(Long userId) {
        int pageN = 1;
        int pageS = 100;
        while (true) {
            Page<UserTree> pages = userTreeDao.pageBySupUserId(userId, pageN, pageS);
            if (null == pages || pages.getList() == null || pages.getList().isEmpty()) {
                break;
            }
            pageN++;

            List<UserTreeH> userTreeHS = new ArrayList<>();
            for (UserTree user : pages.getList()) {
                UserTreeH userTreeH = new UserTreeH();
                userTreeH.setUserId(user.getUserId());
                userTreeH.setSupUserId(user.getSupUserId());
                userTreeH.setLevel(user.getLevel());
                userTreeH.setCreateTime(new Date());
                userTreeHS.add(userTreeH);
            }
            userTreeDao.batchUserTreeH(userTreeHS);
        }

        //只要大于1就代表有数据
        if (pageN > 1) {
            //删除
            userTreeDao.deleteTree(userId);
        }
    }

}
